home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / chat / reflect.000 / reflect / 3.0b3 / refmon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-06  |  6.3 KB  |  266 lines

  1. /*
  2.  
  3. Copyright 1993, 1994, Cornell University
  4.  
  5. Cornell hereby grants permission to use, copy, modify, and distribute this program for any purpose 
  6. and without fee, provided that these copyright and permission notices appear on all copies and 
  7. supporting documentation, the name of Cornell not be used in advertising or publicity pertaining 
  8. to distribution of the program without specific prior permission, notice be given in supporting 
  9. documentation that copying and distribution is by permission of Cornell.  CORNELL MAKES NO 
  10. REPRESENTATIONS OR WARRANTEES, EXPRESS OR IMPLIED.  By way of example, but not limitation, 
  11. CORNELL MAKES NO REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR 
  12. PURPOSE OR THAT THE USE OF THIS SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, 
  13. TRADEMARKS, OR OTHER RIGHTS.  Cornell shall not be held liable for any liability with respect to 
  14. any claim by the user or any other party arising from use of the program.
  15.  
  16. This material is partially based on work sponsored by the National Science Foundation under Cooperative 
  17. Agreement No. NCR-9318337.  The government has certain rights in this material.
  18.  
  19. */
  20.  
  21.  
  22.  
  23. #include <stdio.h>
  24. #include <signal.h>
  25. #include <sys/types.h>
  26. #include <sys/socket.h>
  27.  
  28. #ifndef LINUX
  29. #include <sys/socketvar.h>
  30. #endif
  31.  
  32. #include <netinet/in.h>
  33. #include <netdb.h>
  34.  
  35. #include "reflect.h"
  36. #include "refmon.h"
  37. #include "globals.h"
  38.  
  39. #define INPUTBUFSIZE 100
  40.  
  41. RefConPkt               pkt;
  42. int                   cntrl_sock;
  43.  
  44. main(argc, argv)
  45.     int              argc;
  46.     char               *argv[];
  47. {
  48.     short               sw,len,acnt;
  49.     char                rhost[64],buf[INPUTBUFSIZE];
  50.     char                a1[INPUTBUFSIZE], a2[INPUTBUFSIZE], a3[INPUTBUFSIZE];
  51.     struct sockaddr_in  raddr;
  52.     struct hostent      *hptr;
  53.     extern char         *optarg;
  54.  
  55.     while ((sw = getopt(argc, argv, "s:")) != -1)
  56.        switch (sw) 
  57.        {
  58.           case 's':
  59.              strcpy(rhost, optarg);
  60.              if ((hptr = gethostbyname(rhost)) == NULL)
  61.              {
  62.                 fprintf(stderr,"%s: unknown host\n",rhost);
  63.                 exit(1);    
  64.              }
  65.              break;
  66.  
  67.           case 'h':
  68.           case '?':
  69.              fprintf(stderr,"usage: %s -s hostname\n",argv[0]);
  70.              exit(1);
  71.        }
  72.  
  73.     if (hptr == NULL)
  74.     {
  75.        fprintf(stderr,"usage: %s -s hostname\n",argv[0]);
  76.        exit(1);
  77.     }
  78.  
  79.     if ((cntrl_sock = socket(AF_INET,SOCK_STREAM,0)) == 0)
  80.     {
  81.        perror("opening control socket");
  82.        exit(1);
  83.     }
  84.  
  85.     raddr.sin_family = AF_INET;
  86.     bcopy(hptr->h_addr,&raddr.sin_addr,hptr->h_length);
  87.     raddr.sin_port = htons(CONTROL_PORT);
  88.  
  89.     fprintf(stderr,"Waiting for connection to %s ... ",inet_ntoa(raddr.sin_addr));
  90.  
  91.     if (connect(cntrl_sock,&raddr,sizeof(raddr)) == -1)
  92.     {
  93.        perror("connect");
  94.        exit(1);
  95.     }
  96.     fprintf(stderr,"Connected \n");
  97.  
  98.  
  99.     while (1)
  100.     {
  101.        get_input(buf);
  102.        acnt = sscanf(buf,"%s %s %s",a1,a2,a3);
  103.  
  104.        if (strcmp(a1,"quit") == 0)
  105.        {
  106.           close(cntrl_sock);
  107.           exit(0);
  108.        }
  109.  
  110.        if (strcmp(a1,"version") == 0)
  111.        {
  112.           pkt.msg_type = htons(VERSION);
  113.           pkt.msg_len = htons(MINREFPKT);
  114.           query(); 
  115.           printf("%s\n",pkt.msg_data);
  116.           continue;
  117.        }
  118.  
  119.        if (strcmp(a1,"who") == 0)
  120.        {
  121.           pkt.msg_type = htons(WHO);
  122.           pkt.msg_len = htons(MINREFPKT);
  123.           query(); 
  124.           printf("%s\n",pkt.msg_data);
  125.           continue;
  126.        }
  127.  
  128.        if (strcmp(a1,"maven") == 0)
  129.        {
  130.           pkt.msg_type = htons(MAVEN);
  131.           pkt.msg_len = htons(MINREFPKT);
  132.           query(); 
  133.           printf("%s\n",pkt.msg_data);
  134.           continue;
  135.        }
  136.  
  137.        if (strcmp(a1,"uptime") == 0)
  138.        {
  139.           pkt.msg_type = htons(UPTIME);
  140.           pkt.msg_len = htons(MINREFPKT);
  141.           query(); 
  142.           printf("%s\n",pkt.msg_data);
  143.           continue;
  144.        }
  145.        if (strcmp(a1,"term") == 0)
  146.        {
  147.           pkt.msg_type = htons(TERM);
  148.           pkt.msg_len = htons(MINREFPKT);
  149.           query(); 
  150.           printf("%s\n",pkt.msg_data);
  151.           continue;
  152.        }
  153.  
  154.        if (strcmp(a1,"param") == 0)
  155.        {
  156.           pkt.msg_type = htons(PARAM);
  157.           pkt.msg_len = htons(MINREFPKT);
  158.           query(); 
  159.           printf("%s\n",pkt.msg_data);
  160.           continue;
  161.        }
  162.  
  163.        if (strcmp(a1,"help") == 0)
  164.        {
  165.           fprintf(stdout,"valid commands are: quit version help who term param maven uptime\n");
  166.           continue;
  167.        }
  168.  
  169.        fprintf(stdout,"command not understood\n");
  170.        fprintf(stdout,"valid commands are: quit version help who term param maven uptime\n");
  171.  
  172.     }
  173. }
  174.  
  175.  
  176.  
  177. get_input(buf)
  178.     char *buf;
  179. {
  180.     short len;
  181.     char *tmp;
  182.  
  183.     while (1)
  184.     {
  185.        len = 0;
  186.        fprintf(stdout,"> ");
  187.        bzero(buf,INPUTBUFSIZE);
  188.        tmp = buf;
  189.  
  190.        while (1)
  191.        {
  192.           fscanf(stdin,"%c",tmp);
  193.           if ((len == 0) && (*tmp == '\n'))
  194.              break;
  195.           if ((len != 0) && (*tmp == '\n'))
  196.           {
  197.              *tmp = 0;
  198.              return;
  199.           }
  200.           if (len++ == INPUTBUFSIZE)
  201.           {
  202.              fprintf(stdout,"\nInput line too long\n");
  203.              break;
  204.           }
  205.           tmp++;
  206.        }
  207.     }    
  208. }
  209.  
  210.  
  211. query()
  212. {
  213.     RefConPkt         reply_pkt;
  214.     char              *tmp;
  215.     short             len,remainder;
  216.  
  217.     if (write(cntrl_sock,&pkt,ntohs(pkt.msg_len)) == -1)
  218.     {
  219.        perror("write");
  220.        exit(1);
  221.     }
  222.  
  223.     if ((len = read(cntrl_sock,&reply_pkt,sizeof(reply_pkt))) == -1)
  224.     {
  225.        perror("read");
  226.        exit(1);
  227.     }
  228.  
  229.     if (len == 0)
  230.     {
  231.        fprintf(stderr,"connection is closed\n");
  232.        exit(1);
  233.     }
  234.  
  235.     if (len < MINREFPKT)
  236.     {
  237.        fprintf(stderr,"len is less then MINREFPKT\n");
  238.        exit(1);
  239.     }
  240.  
  241.  
  242.     if (len == ntohs(reply_pkt.msg_len))
  243.     {
  244.        bcopy(&reply_pkt,&pkt,ntohs(reply_pkt.msg_len));
  245.        pkt.msg_data[ntohs(reply_pkt.msg_len) - MINREFPKT] = 0;
  246.        return;
  247.     }
  248.  
  249.     remainder = ntohs(reply_pkt.msg_len) - len;
  250.     tmp = ((char *) &reply_pkt) + len;
  251.  
  252.     while (remainder != 0)
  253.     {
  254.        if ((len = read(cntrl_sock,tmp,sizeof(reply_pkt))) == -1)
  255.        {
  256.           perror("read");
  257.           exit(1);
  258.        }
  259.        tmp += len;
  260.        remainder -= len;
  261.     }
  262.  
  263.     bcopy(&reply_pkt,&pkt,ntohs(reply_pkt.msg_len));
  264.     pkt.msg_data[ntohs(reply_pkt.msg_len) - MINREFPKT] = 0;
  265. }
  266.